package
{
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import org.aswing.BorderLayout;
	import org.aswing.Component;
	import org.aswing.Container;
	import org.aswing.FlowLayout;
	import org.aswing.JComboBox;
	import org.aswing.JLabel;
	import org.aswing.JLoadPane;
	import org.aswing.JOptionPane;
	import org.aswing.JPanel;
	import org.aswing.JWindow;
	import org.aswing.SoftBoxLayout;
	import org.aswing.event.InteractiveEvent;
	

	/**
	 * 面板
	 */
	public class CreateTaskPane extends JPanel
	{
		private var cityCombo:JComboBox;
		private var cityLabel:JLabel;
		private var descriptionLabel:JLabel;
		private var temperatureLabel:JLabel;
		private var tomorrowDescriptionLabel:JLabel;
		private var tomorrowTemperatureLabel:JLabel;
		

		private var mainWindow:JWindow = new JWindow();//load pic
		private var cityArr:Object = {};
		private var cityId:String;
		private var _cityNo:int;//城市序号，供重建界面后设置selectIndex用
		private var _cityNoPrev:int;//选择之前的序号
		
		public function CreateTaskPane()
		{
			super();
		}
		
		public function createTaskPaneHandler(_weatherObj:Object, _cityArr:Object):void
		{
			cityArr = _cityArr;
			
			cityCombo = new JComboBox();
			cityCombo.setPreferredWidth(100);
			cityCombo.setListData(_cityArr.names);//赋值
			//cityCombo.setSelectedIndex(0);
			cityCombo.setComBoundsXYWH(0, 0, 100, 300);
			cityCombo.addSelectionListener(setCity);
			cityCombo.setMaximumRowCount(14);//下拉框显示的行数
			cityLabel = new JLabel(_weatherObj.city);
			descriptionLabel = new JLabel(_weatherObj.description);
			temperatureLabel = new JLabel(_weatherObj.temperature);
			tomorrowDescriptionLabel = new JLabel(_weatherObj.tomorrow_description);
			tomorrowTemperatureLabel = new JLabel(_weatherObj.tomorrow_high+" ～ "+_weatherObj.tomorrow_low);
			
			
			setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS, 2));
			//append(labelHold(new JLabel(""), cityLabel.getText(), "城市"));
			append(labelHold(new JLabel(" "), ""));
			append(labelHold(cityCombo, "城市："));
			append(labelHold(new JLabel(" "), ""));
			append(labelHold(new JLabel(""), "今日："));
			append(labelHold(descriptionLabel, "天气", "天气"));
			append(labelHold(temperatureLabel, "温度", "温度"));
			picHold(_weatherObj.pic);
		}
		
		
		private function setCity(event:InteractiveEvent):void
		{
			var item:String = event.target.getSelectedItem();
			var len:int = cityArr.names.length;
			
			for(var i:int=0; i<len; i++)
			{
				if(item == cityArr.obj[i].name)
				{
					if(cityArr.obj[i].id != "")
					{
						this.selectedCityId = cityArr.obj[i].id;
						this.cityNo = i;
						
						cityCombo.removeSelectionListener(setCity);
						dispatchEvent(new Event("CITY_SELECTED"));//根据此事件，取城市id和城市序号
					}
					else
					{
						cityCombo.setSelectedIndex(_cityNoPrev)
						JOptionPane.showMessageDialog("提示", "请选个一个城市");
					}
					break;
				}
			}
			
			
		}	
			
		
		//创建一个容器，左边显示一个指定字符串text的标签，右边显示指定组件c，
		//通过toolTip指定需要显示的提示文字，如果不需要，则传入null
		private function labelHold(c:Component, text:String, toolTip:String=null):Container
		{
			var panel:JPanel=new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 0, false));
			var label:JLabel=new JLabel(text);
			panel.appendAll(label, c);
			if (toolTip != null)
			{
				panel.setToolTipText(toolTip);
			}
			return panel;
		}

		
		//图
		private function picHold(_url:String):void
		{
			var pane:Container = mainWindow.getContentPane();
			pane.setLayout( new BorderLayout());
			pane.setToolTipText(descriptionLabel.getText());//提示
			
			var loadPane:JLoadPane = new JLoadPane();
			pane.append(loadPane, BorderLayout.CENTER);
			
			//mainWindow.setSizeWH(200, 300);
			mainWindow.show();
			loadPane.load(new URLRequest(_url));
			loadPane.addEventListener(Event.COMPLETE, loadCompleteHandler);

		}

		private function loadCompleteHandler(event:Event):void
		{
			append(mainWindow);
			
			append(labelHold(new JLabel(" "), ""));
			append(labelHold(new JLabel(""), "明日："));
			
			append(labelHold(tomorrowDescriptionLabel, "天气", "明日天气"));
			append(labelHold(tomorrowTemperatureLabel, "温度", "明日温度"));
			
		}
		
		public function get city():String //JFrame title
		{
			return cityLabel.getText();
		}
		
		//select city
		public function set selectedCityId(_city:String):void
		{
			cityId = _city;
		}
		public function get selectedCityId():String
		{
			return cityId;
		}
		
		//已选择城市序号
		public function set cityNo(_no:int):void
		{
			_cityNo = _no;
		}
		public function get cityNo():int //选择的城市序号，重建界面用
		{
			return _cityNo;
		}
		
		
		public function setCombIndex(_no:int):void
		{
			_cityNoPrev = _no;
			cityCombo.setSelectedIndex(_no);
		}
		

		
		
		
		
	}
}